package com.github.wxiaoqi.security.crm.core.biz;

import java.math.BigDecimal;
import java.text.Bidi;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.wxiaoqi.security.auth.common.util.SnowFlake;
import com.github.wxiaoqi.security.common.crm.request.AccountQueryRequest;
import com.github.wxiaoqi.security.common.enums.CustomerTypeEnums;
import com.github.wxiaoqi.security.common.msg.ResponseCode;
import com.github.wxiaoqi.security.common.util.EntityUtils;
import com.github.wxiaoqi.security.common.util.MD5;
import com.github.wxiaoqi.security.crm.core.entity.FreezeJournal;
import com.github.wxiaoqi.security.crm.core.entity.MerchantAccount;
import com.github.wxiaoqi.security.crm.core.entity.MerchantAccountJournal;
import com.github.wxiaoqi.security.crm.core.entity.PersonalAccount;
import com.github.wxiaoqi.security.crm.core.entity.PersonalAccountJournal;
import com.github.wxiaoqi.security.crm.core.entity.PersonalBilling;
import com.github.wxiaoqi.security.crm.core.entity.UnfreezeJournal;
import com.github.wxiaoqi.security.crm.core.mapper.FreezeJournalMapper;
import com.github.wxiaoqi.security.crm.core.mapper.MerchantAccountJournalMapper;
import com.github.wxiaoqi.security.crm.core.mapper.MerchantAccountMapper;
import com.github.wxiaoqi.security.crm.core.mapper.PersonalAccountJournalMapper;
import com.github.wxiaoqi.security.crm.core.mapper.PersonalAccountMapper;
import com.github.wxiaoqi.security.crm.core.mapper.PersonalBillingMapper;
import com.github.wxiaoqi.security.crm.core.mapper.UnfreezeJournalMapper;

import lombok.extern.slf4j.Slf4j;

/**
 * 个人或者商户账户信息查询
 */
@Slf4j
@Transactional(rollbackFor = Exception.class)
@Service
public class CommonAccountQueryBiz {
	@Autowired
	private PersonalAccountMapper personalAccountMapper;
	
	@Autowired
	private MerchantAccountMapper merchantAccountMapper;
	
	@Value("${bountyHunter.balanceSaltValue}")
	private String balanceSaltValue;
	/**
	 * 账户查询接口
	 */
	public Map<String, Object> accountQuery(AccountQueryRequest accountQueryRequest) {
		log.info("个人或者商户账户信息查询-请求参数：{}" + EntityUtils.beanToMap(accountQueryRequest));
		Map<String, Object> response = new HashMap<String, Object>();
		try {
			if(CustomerTypeEnums.mer_user.getCode().equals(accountQueryRequest.getCustomerType())){//商户账户
				MerchantAccount queryMerAccountparam=new MerchantAccount();
				queryMerAccountparam.setMerchantId(accountQueryRequest.getMerchantId());
				queryMerAccountparam.setMerchantType(accountQueryRequest.getMerchantType());
				if(!StringUtils.isEmpty(accountQueryRequest.getAccountType())){
					/**
					 * 获取指定账户类型的商户账户信息
					 * */
					queryMerAccountparam.setAccountType(accountQueryRequest.getAccountType());
					MerchantAccount merchantAccount=merchantAccountMapper.selectOne(queryMerAccountparam);
					response.put("accountInfo",merchantAccount);
				}else{
					/***
					 * 获取全部的商户账户信息
					 * */
					List<MerchantAccount>merchantAccounts=merchantAccountMapper.selectByExample(queryMerAccountparam);
					response.put("accountInfo",merchantAccounts);
				}
				
			}
			if(CustomerTypeEnums.per_user.getCode().equals(accountQueryRequest.getCustomerType())){//个人账户
				PersonalAccount queryPerAccountparam=new PersonalAccount();
				queryPerAccountparam.setPlatformId(accountQueryRequest.getPlatformId());
				queryPerAccountparam.setPersonalId(accountQueryRequest.getPersonalId());
				if(!StringUtils.isEmpty(accountQueryRequest.getAccountType())){
					queryPerAccountparam.setAccountType(accountQueryRequest.getAccountType());
					PersonalAccount personalAccount=personalAccountMapper.selectOne(queryPerAccountparam);
					response.put("accountInfo",personalAccount);
				}else{
					List<PersonalAccount>personalAccounts=personalAccountMapper.select(queryPerAccountparam);
					response.put("accountInfo",personalAccounts);
				}
			}
			response.put("code", ResponseCode.OK.getCode());
			response.put("msg", ResponseCode.OK.getMessage());
			log.info("个人或者商户账户信息查询 返回参数。。。。。。{}", response);
		} catch (Exception e) {
			e.printStackTrace();
			log.info("个人或者商户账户信息查询  报错--"+e.getMessage());
			response.put("code", ResponseCode.ABNORMAL_FIELDS.getCode());
			response.put("msg", ResponseCode.ABNORMAL_FIELDS.getMessage());
			return response;
		}
		return response;
	}
	
}
